package com.yxq.mdt.app.modules.order.ui.adapter;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.orhanobut.hawk.Hawk;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.modules.logistics.ui.LogisticsActivity;
import com.yxq.mdt.app.modules.main.bean.NameAndPhoneBean;
import com.yxq.mdt.app.modules.order.bean.EventOrderMsg;
import com.yxq.mdt.app.modules.order.bean.OrderListBean;
import com.yxq.mdt.app.modules.order.model.OrderModel;
import com.yxq.mdt.app.modules.order.model.OrderStatusUtils;
import com.yxq.mdt.app.modules.order.ui.OrderCommentActivity;
import com.yxq.mdt.app.modules.order.ui.OrderDetailActivity;
import com.yxq.mdt.app.modules.order.ui.OrderPayActivity;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.utils.GridSpacingItemDecoration;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Timer;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

/**
 * @author L
 * @time 2019-11-01
 * @describe 我的订单订单列表$
 */
public class OrderListAdapter extends BaseQuickAdapter<OrderListBean.ListBean, BaseViewHolder> {
    Timer timer = null;
    float startX = 0;
    float startY = 0;
    private static boolean isLongClickModule = false;
    private static boolean isClickModule = false;
    private Handler mHandler;
    private long startTime = 0;

    public interface OrderListInterface {
        void refund(int position);
    }

    OrderListInterface orderListInterface;

    public void setOrderListInterface(OrderListInterface orderListInterface) {
        this.orderListInterface = orderListInterface;
    }

    public OrderListAdapter(int layoutResId, @Nullable List<OrderListBean.ListBean> data) {
        super(layoutResId, data);
        mHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderListBean.ListBean item) {
        String status = OrderStatusUtils.status(item.getIs_pay(), item.getIs_send(), item.getIs_confirm(), item.getIs_comment(), item.getRefund_type());
        helper.setText(R.id.order_tv_date, item.getAddtime());
        helper.setText(R.id.order_tv_status, status);
        int num = 0;
        for (OrderListBean.ListBean.GoodsListBean bean : item.getGoods_list()) {
            num += bean.getNum();
        }
        helper.setText(R.id.order_tv_count, String.format("共%d件商品", num));
        helper.setText(R.id.order_tv_price, String.format("￥%.2f", item.getPay_price()));
        helper.setText(R.id.order_tv_price_hint, "已付款");
        //设置按钮状态
        switch (status) {
            case "待付款":
                helper.setText(R.id.order_tv_price_hint, "待付款");
                helper.setTextColor(R.id.order_tv_status, mContext.getResources().getColor(R.color.color_font_red));
                helper.setText(R.id.order_tv_price, String.format("￥%.2f", item.getPay_price()));
                helper.setTextColor(R.id.order_tv_price, mContext.getResources().getColor(R.color.color_font_red));
                helper.setGone(R.id.order_list_layout_button, true);
                helper.setGone(R.id.order_list_tv_button_1, true);
                helper.setGone(R.id.order_list_tv_button_2, false);
                helper.setGone(R.id.order_list_tv_button_3, true);
                helper.setText(R.id.order_list_tv_button_1, "取消订单");
                helper.setText(R.id.order_list_tv_button_3, "立即付款");
                helper.getView(R.id.order_list_tv_button_1).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //取消订单
                        OrderModel.revoke(item.getOrder_id(), new ApiModel.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String s) {
                                ToastUtils.showShort(s);
                                EventBus.getDefault().post(new EventOrderMsg(1));
                            }

                            @Override
                            public void onError(String errMsg) {
                                ToastUtils.showShort(errMsg);
                            }
                        });
                    }
                });
                helper.getView(R.id.order_list_tv_button_3).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //跳转支付
                        Intent intent = new Intent(mContext, OrderPayActivity.class);
                        NameAndPhoneBean nameAndPhoneBean = Hawk.get(SPKey.SP_KEY_NAME_PHONE_INFO);
                        if ((nameAndPhoneBean.getPmSetting().getGift_goods_id() + "").equals(item.getGoods_list().get(0).getGoods_id() + "")) {
                            intent.putExtra("gift", true);
                        }
                        intent.putExtra("orderId", item.getOrder_id());
                        intent.putExtra("price", String.format("%.2f", item.getPay_price()));
                        mContext.startActivity(intent);
                    }
                });
                break;
            case "待发货":
                helper.setTextColor(R.id.order_tv_status, mContext.getResources().getColor(R.color.color_font_black_666));
                helper.setTextColor(R.id.order_tv_price, mContext.getResources().getColor(R.color.color_font_black));
                helper.setTextColor(R.id.order_tv_status, mContext.getResources().getColor(R.color.color_font_black));
                helper.setGone(R.id.order_list_tv_button_1, false);
                helper.setGone(R.id.order_list_tv_button_2, false);
                if (item.getApply_delete() == 1) {
                    //删除
                    helper.setGone(R.id.order_list_layout_button, false);
                    helper.setGone(R.id.order_list_tv_button_3, false);
                } else {
                    helper.setGone(R.id.order_list_layout_button, false);
                    helper.setGone(R.id.order_list_tv_button_3, true);
                    helper.setText(R.id.order_list_tv_button_3, "申请退款");
                    helper.getView(R.id.order_list_tv_button_3).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //申请退款
                            CostumerDialog dialog = new CostumerDialog.Builder(mContext, R.style.custom_dialog_no_match)
                                    .setContentView(R.layout.dialog_clear_cache_layout)
                                    .setCancelable(true)
                                    .create();

                            TextView title = dialog.findViewById(R.id.tv_title);

                            title.setText("");

                            TextView content = dialog.findViewById(R.id.tv_content);
                            content.setText("确定申请退款？");

                            //继续
                            TextView sure = dialog.findViewById(R.id.tv_sure);
                            sure.setText("确定");
                            sure.setOnClickListener(v -> {
                                dialog.dismiss();
                                OrderModel.revoke(item.getOrder_id(), new ApiModel.CommonCallback<String>() {
                                    @Override
                                    public void onSuccess(String s) {
                                        ToastUtils.showShort("已提交");
                                        EventBus.getDefault().post(new EventOrderMsg(2));
                                    }

                                    @Override
                                    public void onError(String errMsg) {
                                        ToastUtils.showShort(errMsg);
                                    }
                                });
                            });

                            //取消
                            TextView cancel = dialog.findViewById(R.id.tv_cancel);
                            cancel.setText("取消");
                            cancel.setOnClickListener(v -> {
                                dialog.dismiss();
                            });

                            dialog.show();

                        }
                    });
                }
                break;
            case "待收货":
                helper.setTextColor(R.id.order_tv_status, mContext.getResources().getColor(R.color.color_font_black_666));
                helper.setTextColor(R.id.order_tv_price, mContext.getResources().getColor(R.color.color_font_black));
                helper.setTextColor(R.id.order_tv_status, mContext.getResources().getColor(R.color.color_font_black));
                helper.setGone(R.id.order_list_layout_button, true);
                helper.setGone(R.id.order_list_tv_button_1, item.isRefund());//是否售后
                helper.setGone(R.id.order_list_tv_button_2, true);
                helper.setGone(R.id.order_list_tv_button_3, true);
                helper.setText(R.id.order_list_tv_button_1, "售后详情");
                helper.setText(R.id.order_list_tv_button_2, "查看物流");
                helper.setText(R.id.order_list_tv_button_3, "确认收货");

                helper.getView(R.id.order_list_tv_button_1).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (item.isRefund()) {
                            //是售后订单 点击后切换到售后订单列表
                            if (orderListInterface != null) {
                                orderListInterface.refund(helper.getAdapterPosition());
                            }
                        } else {
                            //否则跳转订单详情
                            Intent intent = new Intent(mContext, OrderDetailActivity.class);
                            intent.putExtra("orderId", item.getOrder_id());
                            mContext.startActivity(intent);
                        }

                    }
                });
                helper.getView(R.id.order_list_tv_button_2).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //物流页面
                        Intent intent = new Intent(mContext, LogisticsActivity.class);
                        intent.putExtra("orderId", item.getOrder_id());
                        intent.putExtra("orderType", 1);
                        mContext.startActivity(intent);
                    }
                });
                helper.getView(R.id.order_list_tv_button_3).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //确认收货
                        OrderModel.confirm(item.getOrder_id(), new ApiModel.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String s) {
                                ToastUtils.showShort(s);
                                EventBus.getDefault().post(new EventOrderMsg(3));
                                EventBus.getDefault().post(new EventOrderMsg(4));
                            }

                            @Override
                            public void onError(String errMsg) {
                                ToastUtils.showShort(errMsg);
                            }
                        });
                    }
                });
                break;
            case "待评价":
                helper.setTextColor(R.id.order_tv_status, mContext.getResources().getColor(R.color.color_font_black_666));
                helper.setTextColor(R.id.order_tv_price, mContext.getResources().getColor(R.color.color_font_black));
                helper.setTextColor(R.id.order_tv_status, mContext.getResources().getColor(R.color.color_font_black));
                helper.setGone(R.id.order_list_layout_button, true);
                helper.setGone(R.id.order_list_tv_button_1, false);
                helper.setGone(R.id.order_list_tv_button_2, false);
                helper.setGone(R.id.order_list_tv_button_3, true);
                helper.setText(R.id.order_list_tv_button_3, "立即评价");

                helper.getView(R.id.order_list_tv_button_3).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //跳转订单评价页面
                        Intent intent = new Intent(mContext, OrderCommentActivity.class);
                        intent.putExtra("orderId", item.getOrder_id());
                        mContext.startActivity(intent);
                    }
                });
                break;
            case "已完成":
                helper.setTextColor(R.id.order_tv_status, mContext.getResources().getColor(R.color.color_font_black_666));
                helper.setTextColor(R.id.order_tv_price, mContext.getResources().getColor(R.color.color_font_black));
                helper.setTextColor(R.id.order_tv_status, mContext.getResources().getColor(R.color.color_font_black));
                helper.setGone(R.id.order_list_layout_button, false);
                helper.setGone(R.id.order_list_tv_button_1, false);
                helper.setGone(R.id.order_list_tv_button_2, false);
                helper.setGone(R.id.order_list_tv_button_3, false);
                break;
            default:
                helper.setTextColor(R.id.order_tv_status, mContext.getResources().getColor(R.color.color_font_black_666));
                break;
        }
        RecyclerView recyclerView = helper.getView(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 5));
        OrderListGoodsListAdapter orderListGoodsListAdapter = new OrderListGoodsListAdapter(R.layout.layout_order_confirm_goods_list_item_layout, item.getGoods_list());
        recyclerView.setAdapter(orderListGoodsListAdapter);
        if (recyclerView.getItemDecorationCount() == 0) {
            recyclerView.addItemDecoration(new GridSpacingItemDecoration(5, 8, false));
        }
        recyclerView.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                isLongClickModule = false;
                startTime = 0;
                startX = event.getX();
                startY = event.getY();
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (v.getId() != 0 && Math.abs(startX - event.getX()) <= 5 && Math.abs(startY - event.getY()) <= 5) {
                    //recyclerView空白处点击事件
                    if (!isLongClickModule) {
                        Intent intent = new Intent(mContext, OrderDetailActivity.class);
                        intent.putExtra("orderId", item.getOrder_id());
                        startActivity(intent);
                    }
                }
            }

            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                if (event.getEventTime() - event.getDownTime() >= 900 && !isLongClickModule) {
                    if (status.equals("待付款") || status.equals("已完成")) {
                        isLongClickModule = true;
                        //添加你长按之后的方法
                        CostumerDialog dialog = new CostumerDialog.Builder(mContext, R.style.custom_dialog_no_match)
                                .setContentView(R.layout.dialog_clear_cache_layout)
                                .setCancelable(false)
                                .create();

                        TextView title = dialog.findViewById(R.id.tv_title);

                        title.setText("提示");

                        TextView content = dialog.findViewById(R.id.tv_content);
                        content.setText("确定删除订单？");

                        //继续
                        TextView sure = dialog.findViewById(R.id.tv_sure);
                        sure.setText("确定");
                        sure.setOnClickListener(v2 -> {
                            dialog.dismiss();
                            isLongClickModule = false;
                            OrderModel.delete(item.getOrder_id(), new ApiModel.CommonCallback<String>() {
                                @Override
                                public void onSuccess(String s) {
                                    ToastUtils.showShort("删除成功");
                                    remove(helper.getAdapterPosition());
                                    EventBus.getDefault().post(new EventOrderMsg(1));
                                    EventBus.getDefault().post(new EventOrderMsg(4));
                                }

                                @Override
                                public void onError(String errMsg) {
                                    ToastUtils.showShort(errMsg);
                                }
                            });
                        });
                        //取消
                        TextView cancel = dialog.findViewById(R.id.tv_cancel);
                        cancel.setText("取消");
                        cancel.setOnClickListener(v1 -> {
                            dialog.dismiss();
                            isLongClickModule = false;
                        });
                        dialog.show();
                    }
                }
            }

            return false;
        });

//        recyclerView.setOnTouchListener(new BeautyTouchListener(type -> {
//            switch (type) {
//                case BeautyTouchListener.OnBeautyTouchListener.CLICK:
//                    Intent intent = new Intent(mContext, OrderDetailActivity.class);
//                    intent.putExtra("orderId", item.getOrder_id());
//                    startActivity(intent);
//                    break;
//                case BeautyTouchListener.OnBeautyTouchListener.LONG_MUCH_OUT_MOVE:
//                    if (status.equals("待付款") || status.equals("已完成")) {
//                        //添加你长按之后的方法
//                        CostumerDialog dialog = new CostumerDialog.Builder(mContext, R.style.custom_dialog_no_match)
//                                .setContentView(R.layout.dialog_clear_cache_layout)
//                                .setCancelable(true)
//                                .create();
//
//                        TextView title = dialog.findViewById(R.id.tv_title);
//
//                        title.setText("提示");
//
//                        TextView content = dialog.findViewById(R.id.tv_content);
//                        content.setText("确定删除订单？");
//
//                        //继续
//                        TextView sure = dialog.findViewById(R.id.tv_sure);
//                        sure.setText("确定");
//                        sure.setOnClickListener(v2 -> {
//                            dialog.dismiss();
//                            OrderModel.delete(item.getOrder_id(), new ApiModel.CommonCallback<String>() {
//                                @Override
//                                public void onSuccess(String s) {
//                                    ToastUtils.showShort("删除成功");
//                                    remove(helper.getAdapterPosition());
//                                    EventBus.getDefault().post(new EventOrderMsg(1));
//                                    EventBus.getDefault().post(new EventOrderMsg(4));
//                                }
//
//                                @Override
//                                public void onError(String errMsg) {
//                                    ToastUtils.showShort(errMsg);
//                                }
//                            });
//                        });
//                        //取消
//                        TextView cancel = dialog.findViewById(R.id.tv_cancel);
//                        cancel.setText("取消");
//                        cancel.setOnClickListener(v1 -> {
//                            dialog.dismiss();
//                        });
//                        dialog.show();
//                    }
//                    break;
//            }
//        }));

//        recyclerView.setOnTouchListener((view, motionEvent) -> {
//            switch (motionEvent.getAction()) {
//                case MotionEvent.ACTION_DOWN:
//                    startX = motionEvent.getX();
//                    startY = motionEvent.getY();
//                    timer = new Timer();
//                    timer.schedule(new TimerTask() {
//                        @Override
//                        public void run() {
//                            isClickModule = false;
//                            isLongClickModule = true;
//                            mHandler.post(() -> {
//                                if (status.equals("待付款") || status.equals("已完成")) {
//
//                                    //添加你长按之后的方法
//                                    CostumerDialog dialog = new CostumerDialog.Builder(mContext, R.style.custom_dialog_no_match)
//                                            .setContentView(R.layout.dialog_clear_cache_layout)
//                                            .setCancelable(true)
//                                            .create();
//
//                                    TextView title = dialog.findViewById(R.id.tv_title);
//
//                                    title.setText("提示");
//
//                                    TextView content = dialog.findViewById(R.id.tv_content);
//                                    content.setText("确定删除订单？");
//
//                                    //继续
//                                    TextView sure = dialog.findViewById(R.id.tv_sure);
//                                    sure.setText("确定");
//                                    sure.setOnClickListener(v2 -> {
//                                        dialog.dismiss();
//                                        OrderModel.delete(item.getOrder_id(), new ApiModel.CommonCallback<String>() {
//                                            @Override
//                                            public void onSuccess(String s) {
//                                                ToastUtils.showShort("删除成功");
//                                                remove(helper.getAdapterPosition());
//                                                EventBus.getDefault().post(new EventOrderMsg(1));
//                                                EventBus.getDefault().post(new EventOrderMsg(4));
//                                            }
//
//                                            @Override
//                                            public void onError(String errMsg) {
//                                                ToastUtils.showShort(errMsg);
//                                            }
//                                        });
//                                    });
//                                    //取消
//                                    TextView cancel = dialog.findViewById(R.id.tv_cancel);
//                                    cancel.setText("取消");
//                                    cancel.setOnClickListener(v1 -> {
//                                        dialog.dismiss();
//                                    });
//                                    if (isLongClickModule) {
//                                        dialog.show();
//                                        timer.cancel();
//                                        timer = null;
//                                    }
//                                }
//                            });
//                        }
//                    }, 1000); // 按下时长设置
//                    break;
//                case MotionEvent.ACTION_MOVE:
//                    double deltaX = Math.sqrt((motionEvent.getX() - startX) * (motionEvent.getX() - startX) + (motionEvent.getY() - startY) * (motionEvent.getY() - startY));
//                    if (deltaX > 10) { // 移动大于10像素
//                        isClickModule = false;
//                        isLongClickModule = false;
//                        if (timer != null) {
//                            timer.cancel();
//                            timer = null;
//                        }
//                    }
//                    break;
//                case MotionEvent.ACTION_UP:
//                    if (timer != null) {
//                        timer.cancel();
//                        timer = null;
//                    }
//                    if (isClickModule) {
//                        //订单详情
//                        Intent intent = new Intent(mContext, OrderDetailActivity.class);
//                        intent.putExtra("orderId", item.getOrder_id());
//                        startActivity(intent);
//                    }
//                    isClickModule = false;
//                    isLongClickModule = false;
//                    break;
//            }
//            return true;
//        });
    }
}
